<?php

require_once 'include/global.inc.php';

// load the game data
load_game($_SESSION['game_id']);
$game = $_SESSION['gamedata'];

// test if this user is part of the game (or a spectator)
if (( ! isset($_SESSION['spectator']) || (false == $_SESSION['spectator'])) && (($_SESSION['player_id'] != $game['white']['id']) && ($_SESSION['player_id'] != $game['black']['id']))){
	header('Location: index.php');
	exit;
}

// save the chat submission before we load the page completely
if (isset($_POST['message']) && ('' != $_POST['message'])){
	// can we post to this box?
	if ((0 != $game['private_chat_box']) && isset($_SESSION['spectator']) && (false != $_SESSION['spectator']))	{
		header('Location: play.php');
		exit;
	}

	$private = (isset($_POST['private']) && ('Yes' == $_POST['private'])) ? 'Yes' : 'No';

	$query = "
		INSERT
		INTO ".T_CHAT."
		SET c_game_id = '{$_SESSION['game_id']}'
			, c_player_id = '{$_SESSION['player_id']}'
			, c_message = '".sani($_POST['message'])."'
			, c_private = '{$private}'
			, c_postdate = NOW( )
	";
	$mysql->query($query);

	// reload so we don't double post
	header('Location: play.php');
	exit;
}

// save the chat box status if it's changed
if (isset($_POST['private'])){
	$private = (int) $_POST['private'];

	$query = "
		UPDATE ".T_GAME."
		SET g_private_chat_box = '{$private}'
		WHERE g_id = '{$_SESSION['game_id']}'
	";
	$mysql->query($query);

	// reload so we don't double post
	header('Location: play.php');
	exit;
}

$board = create_empty_board($game['board_size']);
list($board, $oldboard, $passes, $resign, $bprisoners, $wprisoners, $mstonex, $mstoney, $history, $number_of_moves)
	= play_moves($game['moves'], $board, $game['board_size'], $game['handicap']);

// are we passing or resigning ?
if (isset($_GET['submit']) && ('' != $_GET['submit']) && $game['player']['move'])
{
	$color = strtolower(substr($game['player']['color'], 0, 1));

	if ('pass' == $_GET['submit'])
	{
		save_game($_SESSION['game_id'], $color . 'xx', $passes);
		header('Location: play.php');
		exit;
	}
	else if ('resign' == $_GET['submit'])
	{
		save_game($_SESSION['game_id'], $color . 'zz', 0);
		header('Location: play.php');
		exit;
	}
}

// are we making a move ?
if (isset($_POST['x']) && ('' != $_POST['x']))
{
	$removed = '';
	$color = strtolower(substr($game['player']['color'], 0, 1));
	$x = (int) substr($_POST['x'], 0 ,2);
	$y = (int) substr($_POST['y'], 0 ,2);

	if (is_legal_move($y, $x, $color) && ! is_ko($y, $x, $color))
	{
		do_move($y, $x, $color);
		$mstonex = $x;
		$mstoney = $y;

		if ($color == 'b')
		{
			$cprint = 'Black';

			if (strlen(trim($removed)) > 0)
			{
				$bprisoners += strlen(trim($removed)) / 3;
			}
		}

		if ($color == 'w')
		{
			$cprint = 'White';

			if (strlen(trim($removed)) > 0)
			{
				$wprisoners += strlen(trim($removed)) / 3;
			}
		}

		array_push($history, strtolower(xtoalt($x)) . ($game['board_size'] - $y));
		++$number_of_moves;
		$removed = save_game($_SESSION['game_id'], $color . chr($y + 97) . chr($x + 97), $passes, $removed);

		do_mail($_SESSION['game_id'], $game['opponent']['email'], $game['player']['username'], 'yourturn');

		// reload so we don't double post
		header('Location: play.php');
		exit;
	}
	else
	{
		$error = 'That move was not a legal move';
	}
}

// relaod the game to get the new data
load_game($_SESSION['game_id']);
$game = $_SESSION['gamedata'];

// get the chat box data
$query = "
	SELECT *
		, p_username
	FROM ".T_CHAT."
		LEFT JOIN ".T_PLAYER."
			ON p_id = c_player_id
	WHERE c_game_id = '{$_SESSION['game_id']}'
		AND
		(
			( c_player_id = '{$_SESSION['player_id']}'
				AND c_private = 'Yes' )
			OR ( c_private = 'No' )
		)
	ORDER BY c_postdate DESC
";
$chat_data = $mysql->fetch_array($query, __LINE__, __FILE__);


// start the page
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<title>iShudan : Play</title>

	<meta name="viewport" id="iphone-viewport" content="width=550; initial-scale=.8">
	<meta http-equiv="Content-Language" content="en-us" />
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<meta http-equiv="Content-Style-Type" content="text/css" />

	<link rel="stylesheet" href="css/iShudan.css" type="text/css" />
	<link rel="stylesheet" href="css/play.css" type="text/css" />

	<script type="text/javascript">//<![CDATA[
		<?php draw_javscript( ); ?>

	//]]></script>
	<script type="text/javascript" src="javascript/game.js"></script>

</head>
<body>
<div id="links"><?php echo $CFG_SITELINKS; ?></div>
<h1>iShudan :: play</h1>
<div id="gamewrapper">
<?php

	draw_board($board, $game['board_size'], $game['player']['move']);
	draw_interface($wprisoners, $bprisoners, $history, $game['black']['username'], $game['white']['username'], $game['player']['move'], $passes, $resign);

?>
</div>
<hr style="clear:both;" />
<?php

	// if we are allowed to see the chat box, show it
	if ((0 == $game['private_chat_box']) || ( ! isset($_SESSION['spectator']) || (false == $_SESSION['spectator'])))
	{
		draw_chatbox($chat_data);
	}

?>
<form name="move" method="post" action=""><input type="hidden" name="x" /><input type="hidden" name="y" /></form>
<?php call('GLOBALS'); ?>
<div id="alerts"></div>
</body>
</html>